import axios from "axios";
import Taro from '@tarojs/taro'
import qs from 'qs'


const Axios = axios.create({
    baseURL: '/api',
    timeout: 30000
})

Axios.interceptors.request.use(
    config => {
        // loading 动画
        // token 重定向
        // 后端需求是否序列化
        return config
    },
    error => {
        console.log('request:', error)
        // loading 关闭
        // 请求超时
        if (error.code === 'ECONNABORTED' && error.message.indexOf('timeout') !== -1) {
            console.log('timeout')
            // return service.request(originalRequest);//例如再重复请求一次
        }
        // 需要重定向到错误页面
        const errorInfo = error.response;
        console.log(errorInfo)
        if (errorInfo) {
            // error =errorInfo.data//页面那边catch的时候就能拿到详细的错误信息,看最下边的Promise.reject
            const errorStatus = errorInfo.status; // 404 403 500 ... 等
            console.log(41, errorStatus)
        }
        return Promise.reject(error)
    }
  )

  Axios.interceptors.response.use(response=> {
    let data;
    if(response.data === undefined) {
        data = response.request.responseText
    } else {
        data = response.data;
    }

    switch (data.code) {
        case 200:
            
            break;
    
        default:
            Taro.showToast({
                title: '请求错误，返回码不是200',
                success: ()=> {
                },
                duration: 1000
            })
            return Promise.reject()
    }
    return data
}, error=> {
    if (error.code === 'ECONNABORTED' && error.message.indexOf('timeout') !== -1) {
        // notification.open({
        //     message: '请求超时',
        //     description: '请检查网络或者稍后再试',
        // });
        Taro.showToast({
            title: '请检查网络或者稍后再试',
            success: ()=> {
            },
            duration: 1000
        })
        // return service.request(originalRequest);//例如再重复请求一次
    }

    if (error && error.response) {
        switch (error.response.status) {
            case 400:
                error.message = '请求错误'
                break
            case 403:
                error.message = '拒绝访问'
                break

            case 404:
                error.message = `请求地址出错: ${error.response.config.url}`
                break

            case 408:
                error.message = '请求超时'
                break

            case 500:
                error.message = '服务器内部错误'
                break
            default:
        }
        // notification.open({
        //     key:error.response.status,
        //     message: '发生错误',
        //     description: error.message,
        // });
        Taro.showToast({
            title: '发生错误',
            success: ()=> {
            },
            duration: 1000
        })
    }

    return Promise.reject(error)
})

const API = {
    get(url, params={}) {
        let commonData = this.commonData ? {
            _cetc_t:new Date().getTime()
        } : {};
        return Axios({
            method: 'get',
            url: url,
            params: {
                ...params,
                ...commonData
            },
            headers: {
                'X-Requested-With': 'XMLHttpRequest',
                // 'requestKey': UUID || '',
                // ...Authorization
            }
        })
    },
    postJson(url, params={}) {
        let commonData = this.commonData ? {
            _cetc_t:new Date().getTime()
        } : {};

        let axios = Axios({
            method: 'post',
            url: url,
            data:{...data, ...commonData},
            headers: {
                'X-Requested-With': 'XMLHttpRequest',
                // 'requestKey': UUID || '',
                'Content-Type': 'application/json',
            }
        })

        return axios;
    }
}

export default API